package org.rcgwt.sample.client.datechooser;

import java.util.Date;

import org.rcgwt.client.ui.calendar.RichCalendar;
import org.rcgwt.client.ui.calendar.model.impl.Event;
import org.rcgwt.client.ui.calendar.synchronization.GoogleAgendaSynchronizer;
import org.rcgwt.client.ui.datechooser.DateChooser;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * DateChooser sample
 * 
 * @author Elian ORIOU
 */
public class DateChooserExample implements EntryPoint {

	private FlexTable rootPanel;

	private DateChooser dateChooser;
	private GoogleAgendaSynchronizer synchronizer;

	private Button b_readOnly;

	private Label l_title;
	private Label l_footer;

	@Override
	public void onModuleLoad() {
		RootPanel rp = RootPanel.get();

		rootPanel = new FlexTable();

		l_title = new Label("DateChooser Component Showcase");
		l_title.addStyleName("dateChooser-test-title");

		dateChooser = new DateChooser();
		dateChooser.setValue(new Date());
		dateChooser.setReadOnly(true);

		RichCalendar cal = dateChooser.getCalendar();
		cal.createEvent(new Event(
				"Saint Sacrement de l'archeveque de Canterbury",
				"05/06/2010 12:30", "25/06/2010 13:00"));

		synchronizer = GWT.create(GoogleAgendaSynchronizer.class);
		synchronizer
				.synchronize(
						"http://www.google.com/calendar/feeds/elian.oriou%40gmail.com/private-0f9c31f124fa919c9e4ad26d5060ba49/basic",
						dateChooser.getCalendar());

		cal.setMinDate(new Date(110, 4, 15));
		cal.setMaxDate(new Date(110, 7, 15));

		b_readOnly = new Button("Switch ReadOnly");
		b_readOnly.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (dateChooser.isReadOnly() == true) {
					dateChooser.setReadOnly(false);
				} else {
					dateChooser.setReadOnly(true);
				}
			}
		});

		l_footer = new Label();
		l_footer.addStyleName("richCalendar-test-title");

		rootPanel.setWidget(0, 1, l_title);
		rootPanel.setWidget(1, 0, dateChooser);
		rootPanel.setWidget(2, 0, b_readOnly);
		rootPanel.setWidget(3, 0, l_footer);

		rp.add(rootPanel);
	}
}
